package com.stream;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @Author zhangqing
 * @Date 2019/6/20 18:51
 **/
public class StreamTest {
    public static void main(String[] args) {
        List<String> nameList = Arrays.asList("张三","王五","李四","王麻子","","","");
        //stream().filter 过滤方法 将符条件的数据过滤出来
        List<String> filter = nameList.stream().filter(item->item.length()==2).collect(Collectors.toList());
        //生成随机数
        Random random = new Random();
        //随机数遍历 limit() 获取指定数量的流
        System.out.println("---------遍历");
        random.ints().limit(10).forEach(System.out::println);
        List<Integer> numberList = Arrays.asList(1,2,4,2,3,123,315,52,13,5234,123);
        //stream().map 映射元素到到对应的结果
        List<Integer> pingfang = numberList.stream().map(i->i*i).collect(Collectors.toList());
        System.out.println("===========排序========");
        //sorted() 排序
        random.ints().limit(10).sorted().forEach(System.out::println);
        //并行流 parallelStream() Collectors.toList 将流转化成集合
        List<String> parallel = nameList.parallelStream().filter(item->item.isEmpty()).collect(Collectors.toList());
        //Collectors.joinning 将流合并成字符串
        String merge = nameList.stream().filter(item->!item.isEmpty()).collect(Collectors.joining("&&"));
        //mapToInt() 统计操作 类似mysql的聚合函数 可以统计数量 最大值，最小值，求和。
        IntSummaryStatistics stats = numberList.stream()
                .mapToInt((x)->x).summaryStatistics();


    }
}
